#include "usermanager.h"

#include <QLineEdit>
#include <QComboBox>
#include <QPushButton>
#include <QListWidget>
#include <QMessageBox>
#include <QDebug>

// ====================
// 构造函数
// ====================
UserManager::UserManager(QLineEdit *input,
                         QComboBox *combo,
                         QPushButton *addBtn,
                         QPushButton *delBtn,
                         QListWidget *listWidgetHide,
                         QObject *parent)
    : QObject(parent),
      m_input(input),
      m_comboBox(combo),
      m_addButton(addBtn),
      m_deleteButton(delBtn),
      m_listWidgetHide(listWidgetHide)
{
    connect(m_addButton, SIGNAL(clicked()), this, SLOT(onAddUser()));
    connect(m_deleteButton, SIGNAL(clicked()), this, SLOT(onDeleteUser()));
}
void UserManager::updateUiPath(const QString &uipath)
{
    m_uiPath = uipath;
}

QString UserManager::getCurrentUserInput() const
{
    QString username = m_input->text().trimmed();  // 只取输入框的值

    if (username.isEmpty()) {
        QMessageBox::warning(nullptr, "Warning", "Enter User Name!");
        return QString();
    }

    return username;
}
// 判断 ui.json 是否包含指定用户
bool UserManager::checkUserInUiJson(const QString &username, bool expectExists) const
{
    if (!m_jsonconfigwriter) return false;

    QStringList existing = m_jsonconfigwriter->Match_Users_list();
    bool exists = existing.contains(username);

    if (expectExists && !exists) {
        QMessageBox::warning(nullptr,"Warning",QString("User '1%' Not Exit！").arg(username));
        return false;
    }
    if (!expectExists && exists) {
        QMessageBox::warning(nullptr,"Warning",QString("User '1%' Exit！").arg(username));
        return false;
    }

    return true; // 符合期望
}
QStringList UserManager::currentHideControlsFromUI() const
{
    QStringList hiddenControls;

    if (!m_listWidgetHide)
        return hiddenControls;

    for (int i = 0; i < m_listWidgetHide->count(); ++i) {
        QListWidgetItem *item = m_listWidgetHide->item(i);
        if (item) {
            hiddenControls << item->text();
        }
    }

    return hiddenControls;
}
void UserManager::onAddUser()
{
    if (m_uiPath.isEmpty()) {
        QMessageBox::warning(nullptr, "Error", "No UI file selected!");
        return;
    }

    JsonConfigWriter writer(nullptr, m_uiPath);  // ⭐ 临时构造

    QString username = getCurrentUserInput();
    if (username.isEmpty()) return;
    QStringList existing = writer.Match_Users_list();
    if (existing.contains(username)) {
        QMessageBox::warning(nullptr,"Warning",
                             QString("User '%1' already exists!").arg(username));
        return;
    }

    if (!writer.Write_To_Ui_Json(username, QStringList())) {
        QMessageBox::warning(nullptr, "Error", "Writing ui.json Failed!");
        return;
    }

    writer.Write_To_User_Json(username);

    m_comboBox->addItem(username);
    m_input->clear();

    emit switchToUser(username);

    QMessageBox::information(nullptr, "Success",
                              QString("Create User '%1'").arg(username));
    qDebug() << "uiPath in UserManager =" << m_uiPath;
}
void UserManager::onDeleteUser()
{
    if (m_uiPath.isEmpty()) {
        QMessageBox::warning(nullptr, "Error", "No UI file selected!");
        return;
    }

    JsonConfigWriter writer(nullptr, m_uiPath);  // ⭐ 临时构造

    QString username = getCurrentUserInput();
    if (username.isEmpty()) return;
    QStringList existing = writer.Match_Users_list();
    if (!existing.contains(username)) {
        QMessageBox::warning(nullptr, "Warning",
                             QString("User '%1' not exists!").arg(username));
        return;
    }

    if (!writer.remove_User_From_Ui_Json(username)) {
        QMessageBox::warning(nullptr, "Error", "Delete User Failed!");
        return;
    }

    int idx = m_comboBox->findText(username);
    if (idx >= 0)
        m_comboBox->removeItem(idx);

    m_input->clear();

    if (m_comboBox->count() > 0) {
        m_comboBox->setCurrentIndex(0);
        emit switchToUser(m_comboBox->currentText());
    }

    emit userDeleted(username);

    QMessageBox::information(nullptr, "Success",
                             QString("Delete User '%1'").arg(username));
    qDebug() << "makeJsonPath, m_uiFilePath =" << m_uiPath
             << " => result path =" << writer.makeJsonPath();
}
